import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-09-19
 * Time: 15:11
 */
public class Test {
    public static int[] sortedSquares(int[] nums) {
        //创建新数组
        int[] ans = new int[nums.length];
        //定义双指针
        int left = 0;
        int right = nums.length - 1;
        int cur = right;
        while(left <= right){
            if(Math.abs(nums[left]) <= Math.abs(nums[right])){
                ans[cur--] = nums[right] * nums[right];
                right--;
            }else{
                ans[cur--] = nums[left] * nums[left];
                left++;
            }
        }
        return ans;
    }
    public static void main(String[] args) {
        int[] nums = {-4,-1,0,3,10};
        int[] ret = sortedSquares(nums);
        System.out.println(Arrays.toString(ret));
    }
}
